package com.example.demo.config;

import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.example.demo.bean.User;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.nio.charset.Charset;

@Configuration
public class WebMvcConfig {

//    @Bean
//    MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(){
//        ObjectMapper objectMapper = new ObjectMapper();
//        objectMapper.setDateFormat(new SimpleDateFormat("YYYY-MM-dd HH:mm:ss"));
//        return new MappingJackson2HttpMessageConverter(objectMapper);
//    }

    @Bean
    @ConditionalOnMissingBean
    User user(){
        System.err.println("missing bean");
        User user = new User();
        user.setUsername("xsd");
        return user;
    }

    /**
     * FastJson没有Jackson、Gson原生支持, 所以需要注入消息转换器
     * @return
     */
    @Bean
    FastJsonHttpMessageConverter fastJsonHttpMessageConverter(){
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig jsonConfig = new FastJsonConfig();
        //- 设置日期格式
        jsonConfig.setDateFormat("YYYY-MM-dd HH:mm:ss");
        converter.setFastJsonConfig(jsonConfig);
        //- 解决乱码
        converter.setDefaultCharset(Charset.forName("utf-8"));
        return converter;
    }

}
